/**
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: 联动优势科技有限公司</p>
 * <p>Sep 7, 201211:44:23 AM</p>
 * @author Ding Zhe
 * @version 1.0
 */
package com.umpay.privi.util;

import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.umpay.privi.common.XmlData;
import com.umpay.privi.ejb.sys.MenuServiceBean;

/** 
 * desc: 请求参数校验工具类
 * <p>创建人：Ding Zhe 创建日期：Sep 7, 2012 </p>
 * @version V1.0  
 */
public class CheckReqDataUtil{
	private static Logger log = LoggerFactory.getLogger(MenuServiceBean.class);
	
	/**
	 * desc: 校验map请求参数
	 * <p>创建人：Ding Zhe , Sep 7, 2012 2:20:14 PM</p>
	 * @param keys 
	 * @param reqMap
	 * @throws Exception
	 */
	public static boolean doCheck(String ruleId, Map<String, String> reqMap) throws Exception {
		if (StringUtil.isEmpty(ruleId)) {
			return true;
		}
		String keys = PropertiesUtil.getValue(ruleId);
		if (StringUtil.isEmpty(keys)) {
			log.info(String.format("Check ruleId %s found no check info", ruleId));
            return true;
		}
		// 对每个key进行正则表达式校验
        String[] rules = keys.split(",");
        
        for(String key:rules){
        	key = StringUtil.trim(key);
    		if (StringUtil.isEmpty(key)) {
    			continue;
    		}
    		
    		boolean must = true;
    		if (isOptional(key)) {
    			key = removBrackets(key);
    			must = false;
    		} 
    		if (!check(key, reqMap.get(key), must)) {
    			log.error(String.format("Request Parameter %s can not match", key));
    			return false;
			}
    		
		}
        return true;
	}
	/**
	 * desc: 校验单个字符串
	 * <p>创建人：Ding Zhe , Sep 7, 2012 2:20:09 PM</p>
	 * @param key
	 * @param reqData
	 * @throws Exception
	 */
	public static boolean doCheck(String key, String reqData) throws Exception {
		key = StringUtil.trim(key);
		if (StringUtil.isEmpty(key)) {
			return true;
		}
		
		if (isOptional(key)) {
			check(removBrackets(key), reqData, false);
		} else {
			check(key, reqData, true);
		}
		return true;
	}
	/**
     * 真正的校验逻辑，通过配置的正则表达式校验字段是否符合规范
     * 
     * @param key 待校验字段和需要放入到DC的目标字段
     * @param dc
     * @param must 字段是否是必须的
     * @throws Exception
     */
	public static boolean check(String key, String value, boolean must) throws Exception {
		if (StringUtil.isEmpty(value)) {
			if (must) {
				log.error(String.format("Request Parameter %s is Empty", key));
				return false;
			}
		} else {
			String regex = PropertiesUtil.getValue("Regex." + key);
			if (StringUtil.isEmpty(regex)) {
				log.warn(String.format("Request Parameter %s found no regex info", key));
				return true;
			} 
			// 不符合正则表达式
            if (!value.matches(regex)) {
            	log.error(String.format("Request Parameter %s[%s] cannot match the regex[%s]", key, value, regex));
            	return false;
            }
		}
		return true;
	}
	
    /**
     * desc: 是否是必须的字段
     * <p>创建人：Ding Zhe , Sep 7, 2012 11:53:53 AM</p>
     * @param key 待校验的Key
     * @return 必须字段返回<tt>false</tt>, 否则返回<tt>true</tt>
     */
	public static boolean isOptional(String key) {
        return key.startsWith("[") && key.endsWith("]");
    }
    /**
     * desc: 去除中括号
     * <p>创建人：Ding Zhe , Sep 7, 2012 11:54:55 AM</p>
     * @param key 待校验的Key
     * @return 去除中括号的字符串
     */
	public static String removBrackets(String key) {
        return key.substring(1, key.length() - 1);
    }
	/**
	 * desc:
	 * <p>创建人：Ding Zhe , Sep 7, 2012 11:44:23 AM</p>
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		Map<String, String> reqMap = new HashMap<String, String>();
		reqMap.put(XmlData.MERID, "asfdas");
		reqMap.put(XmlData.EMAIL, "dingzhe@umpay.com");
		reqMap.put(XmlData.MOBILEID, "阿斯顿发水电费4");
		reqMap.put(XmlData.LOGNAME, "9996");
		
		System.out.println(doCheck("CrmQryServiceBean.queryOpenAccBankByMerId.require", reqMap));
		
	}

}
